package br.com.edumobi.fragments;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import br.com.edumobi.P;
import br.com.edumobi.R;
import br.com.edumobi.dados.modelos.Aluno;

public class SenhaAlunoFragment extends GenericoFragment {

    private TextView textViewNomeTelaSenhaAlunoFragment;
    private EditText editTextSenhaAtualSenhaAlunoFragment;
    private EditText editTextNovaSenhaSenhaAlunoFragment;
    private EditText editTextRepitaSenhaSenhaAlunoFragment;
    private Button buttonAlterarSenhaAlunoFragment;
    private ProgressBar progressBarSenhaAlunoFragment;

    private AcoesSenhaAlunoFragment activity;
    private SenhaAlunoTask task;

    private Aluno aluno;

    private View.OnClickListener clickButtonAlterarSenhaAlunoFragment = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String senhaAtual =  editTextSenhaAtualSenhaAlunoFragment.getText().toString();
            String novaSenha = editTextNovaSenhaSenhaAlunoFragment.getText().toString();
            String confirmacaoNovaSenha = editTextRepitaSenhaSenhaAlunoFragment.getText().toString();

            if (senhaAtual.trim().equals(P.STRING_EMPITY)) {
                activity.falarEExibirTexto(R.string.obrigatoriedade_senha_atual);
            } else if (!senhaAtual.equals(aluno.senha)) {
                activity.falarEExibirTexto(R.string.excecao_senha_atual_invalida);
            } else if (novaSenha.trim().equals(P.STRING_EMPITY)) {
                activity.falarEExibirTexto(R.string.obrigatoriedade_nova_senha);
            } else if (confirmacaoNovaSenha.trim().equals(P.STRING_EMPITY)) {
                activity.falarEExibirTexto(R.string.obrigatoriedade_confirmacao_nova_senha);
            } else if (!novaSenha.equals(confirmacaoNovaSenha)) {
                activity.falarEExibirTexto(R.string.excecao_nova_senha_confirmacao_nao_conferem);
            } else {
                if (activity.conectado()) {
                    aluno.senha = novaSenha;

                    task = new SenhaAlunoTask();
                    task.execute();
                } else {
                    activity.falarEExibirTexto(R.string.excecao_sem_conexao_internet);
                }
            }
        }
    };

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        activity = (AcoesSenhaAlunoFragment) getActivity();

        aluno = activity.obterAluno();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_senha_aluno, container, false);

        textViewNomeTelaSenhaAlunoFragment = (TextView) view.findViewById(R.id.textViewNomeTelaSenhaAlunoFragment);
        editTextSenhaAtualSenhaAlunoFragment = (EditText) view.findViewById(R.id.editTextSenhaAtualSenhaAlunoFragment);
        editTextNovaSenhaSenhaAlunoFragment = (EditText) view.findViewById(R.id.editTextNovaSenhaSenhaAlunoFragment);
        editTextRepitaSenhaSenhaAlunoFragment = (EditText) view.findViewById(R.id.editTextRepitaSenhaSenhaAlunoFragment);
        buttonAlterarSenhaAlunoFragment = (Button) view.findViewById(R.id.buttonAlterarSenhaAlunoFragment);
        progressBarSenhaAlunoFragment = (ProgressBar) view.findViewById(R.id.progressBarSenhaFragment);

        buttonAlterarSenhaAlunoFragment.setOnClickListener(clickButtonAlterarSenhaAlunoFragment);

        if (task != null && task.getStatus().equals(AsyncTask.Status.RUNNING)) {
            esconderComponentes();
            progressBarSenhaAlunoFragment.setVisibility(View.VISIBLE);
        }

        return view;
    }

    @Override
    public void exibirComponentes() {
        textViewNomeTelaSenhaAlunoFragment.setVisibility(View.VISIBLE);
        editTextSenhaAtualSenhaAlunoFragment.setVisibility(View.VISIBLE);
        editTextNovaSenhaSenhaAlunoFragment.setVisibility(View.VISIBLE);
        editTextRepitaSenhaSenhaAlunoFragment.setVisibility(View.VISIBLE);
        buttonAlterarSenhaAlunoFragment.setVisibility(View.VISIBLE);
        progressBarSenhaAlunoFragment.setVisibility(View.VISIBLE);
    }

    @Override
    public void esconderComponentes() {
        textViewNomeTelaSenhaAlunoFragment.setVisibility(View.GONE);
        editTextSenhaAtualSenhaAlunoFragment.setVisibility(View.GONE);
        editTextNovaSenhaSenhaAlunoFragment.setVisibility(View.GONE);
        editTextRepitaSenhaSenhaAlunoFragment.setVisibility(View.GONE);
        buttonAlterarSenhaAlunoFragment.setVisibility(View.GONE);
        progressBarSenhaAlunoFragment.setVisibility(View.GONE);
    }

    private class SenhaAlunoTask extends AsyncTask<Void, Void, Boolean>{

        @Override
        protected void onPreExecute() {
            esconderComponentes();
            progressBarSenhaAlunoFragment.setVisibility(View.VISIBLE);
            activity.falarTexto(R.string.aguarde);
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            return activity.salvarDadosAluno(aluno);
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            exibirComponentes();
            progressBarSenhaAlunoFragment.setVisibility(View.GONE);

            if (aBoolean) {
                activity.falarEExibirTexto(R.string.senha_alterada_sucesso);
            } else {
                activity.falarEExibirTexto(R.string.excecao_autenticacao_mausucedida);
            }
        }
    }

    public interface AcoesSenhaAlunoFragment extends GenericoFragment.AcoesGenericoFragment{
        Aluno obterAluno();
        Boolean salvarDadosAluno(Aluno aluno);
    }
}
